package com.talkweb.sendmsg.controller;

import com.alibaba.fastjson.JSONObject;
import com.talkweb.sendmsg.service.MsgSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * 短信API服务调用示例代码 － 聚合数据
 * 在线接口文档：http://www.juhe.cn/docs/54
 */
@RestController
@RequestMapping("/restAPI")
public class RestTemplateController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private MsgSender msgSender;

	public static final String APPKEY = "2ca1d149bcecff7b1adb49712f718d46";

	/**
	 * HTTP GET method
	 * @return
	 */
	@GetMapping("/testGetApi")
	public String getJson() {
		String result = msgSender.sendMsg();
		System.out.println(result);
		return result;
	}

	@GetMapping("/test")
	public String test() {
		return "test";
	}

	/**
	 * HTTP POST method
	 * @param param
	 * @return
	 */
	@PostMapping(value = "/testPost")
	public Object postJson(@RequestBody JSONObject param) {
		System.out.println(param.toJSONString());
		param.put("action", "post");
		param.put("username", "tester");
		param.put("pwd", "123456748");
		return param;
	}

	@PostMapping(value = "/testPostApi")
	public Object testPost() {
		String url = "";
		JSONObject postData = new JSONObject();
		postData.put("descp", "request for post");
		JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
		return json;
	}
}
